<?php

class BoyLeeController extends Controller
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='/layouts/column2';
	public $pageTitle = '测试版 - 三江培训专用官网';

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('index','spage','tpage'),
				'users'=>array('*'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}



	/**
	 * Lists all models. in index.php page
	 */
	public function actionIndex()
	{
	  //prepare Nav Menu Items
	  //1 Hour cache on it.
	  $cache = Yii::app()->cache;
	  $navResults = $cache->get('NavMenu');
	  if ($navResults === false){
	      $navResults = NavMenu::model()->findAll('status_id = 1 limit 0, 6');
	      $cache->set('NavMenu', $navResults, 60*60);
	  }
	  
	  //get one bg image name by random
	  //cache embed in function
	  $bgImageResults = BgImage::model()->getRandomBgImg();
	  
	  //get second Nav Items as an Array
	  //1 hour cache
	  $newsTypeResults = $cache->get('NewsTypes');
	  if ($newsTypeResults === false){ 
    	  $newsTypeResults = NewsType::model()->getNewsTypeList();
    	  $newsTypeResults[998] = '下载';
    	  $cache->set('NewsTypes', $newsTypeResults, 60*60);
	  }
	  
	  //get gonggao contents
	  $indexGongGaoResults = News::model()->getLastNewsByType('1');
	  //get News contents
	  $indexNewsResults = News::model()->getLastNewsByType('2');
	  
	  //get ImgLib data
	  //1 hour cache
	  $imgLibResults = $cache->get('IndexImageLib');
	  if ($imgLibResults === false){
	      $imgLibResults = ImgLib::model()->getLastImg();
	      $cache->set('IndexImageLib', $imgLibResults, 60*60);
	  }
	  
	  //get Download files
	  $downloadResults = Download::model()->getLastDownloadItems();
	  
	  //test part
//	  echo '<pre>';
//	  print_r($downloadResults);
	  
		$this->render('siteindex',array(
		    'dataNavs'=>$navResults,
		    'dataSecondNavs'=>$newsTypeResults,
		    'dataBgImgName'=>$bgImageResults,
		    'dataGonggao'=>$indexGongGaoResults,
		    'dataNews'=>$indexNewsResults,
		    'dataImgLib'=>$imgLibResults,
		    'dataDownload'=>$downloadResults
		));
	}

	
	
	
	/**
	 * Lists type models. in spage.php page
	 */
	public function actionSPage($id)
	{
	  $id = (int)$id;
	  if ($id ==0) $id = 1;
	  if ($id  == 998){
	      //TODO Down
	      $criteria =  new CDbCriteria();
	      $criteria->condition = 'status_id = 1 ';
	      $criteria->order = 'id DESC';
	      
	      $spageResult = new CActiveDataProvider('Download',array(
	      	'criteria'=>$criteria,
	        'pagination'=>array('pageSize'=>5),
	      ));
	  }else {
	      //TODO News
	      $criteria =  new CDbCriteria();
	      $criteria->condition = 'status_id = 1 and type_id =:type_id';
	      $criteria->params[':type_id'] = $id;
	      $criteria->order = 'create_time DESC';
	      
	      $spageResult = new CActiveDataProvider('News',array(
	      	'criteria'=>$criteria,
	        'pagination'=>array('pageSize'=>5),
	      ));
	  }
	//prepare Nav Menu Items
	  //1 Hour cache on it.
	  $cache = Yii::app()->cache;
	  $navResults = $cache->get('NavMenu');
	  if ($navResults === false){
	      $navResults = NavMenu::model()->findAll('status_id = 1 limit 0, 6');
	      $cache->set('NavMenu', $navResults, 60*60);
	  }
	  
	  //get one bg image name by random
	  //cache embed in function
	  $bgImageResults = BgImage::model()->getRandomBgImg();
	  
	  //get second Nav Items as an Array
	  //1 hour cache
	  $newsTypeResults = $cache->get('NewsTypes');
	  if ($newsTypeResults === false){ 
    	  $newsTypeResults = NewsType::model()->getNewsTypeList();
    	  $newsTypeResults[998] = '下载';
    	  $cache->set('NewsTypes', $newsTypeResults, 60*60);
	  }
	  
	  
	  
	  //test part
//	  echo '<pre>';
//	  print_r($downloadResults);
	      
		$this->render('spage',array(
		    'dataNavs'=>$navResults,
		    'dataSecondNavs'=>$newsTypeResults,
		    'dataBgImgName'=>$bgImageResults,
		    'dataSpageMain'=>$spageResult,
		    'id'=>$id,
		));
	}

	/**
	 * Lists type models. in spage.php page
	 */
	public function actionTPage($id)
	{
	  $id = (int)$id;
	  if ($id ==0) $id = 1;
	  
	  $newResult = News::model()->find('id = :id',array(':id'=>$id));
	  $this->pageTitle .= ' - '.$newResult->news_title;
	  
	//prepare Nav Menu Items
	  //1 Hour cache on it.
	  $cache = Yii::app()->cache;
	  $navResults = $cache->get('NavMenu');
	  if ($navResults === false){
	      $navResults = NavMenu::model()->findAll('status_id = 1 limit 0, 6');
	      $cache->set('NavMenu', $navResults, 60*60);
	  }
	  
	  //get one bg image name by random
	  //cache embed in function
	  $bgImageResults = BgImage::model()->getRandomBgImg();
	  
	  //get second Nav Items as an Array
	  //1 hour cache
	  $newsTypeResults = $cache->get('NewsTypes');
	  if ($newsTypeResults === false){ 
    	  $newsTypeResults = NewsType::model()->getNewsTypeList();
    	  $newsTypeResults[998] = '下载';
    	  $cache->set('NewsTypes', $newsTypeResults, 60*60);
	  }
	  
	  
	  //test part
//	  echo '<pre>';
//	  print_r($newResult);
	      
		$this->render('tpage',array(
		    'dataNavs'=>$navResults,
		    'dataSecondNavs'=>$newsTypeResults,
		    'dataBgImgName'=>$bgImageResults,
		    'dataNew'=>$newResult,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=User::model()->findByPk((int)$id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='user-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}
